package com.franklin.ideaplugin.maphelper.utils;

import com.franklin.ideaplugin.api.beans.variable.IVariable;
import com.franklin.ideaplugin.maphelper.beans.colmember.IColMember;
import com.franklin.ideaplugin.maphelper.col.declaration.IColDeclaration;
import com.franklin.ideaplugin.maphelper.col.operation.ColOperationFactory;
import com.franklin.ideaplugin.maphelper.resolver.impl.collection.CollectionMemberResolverFactory;
import com.intellij.psi.PsiCodeBlock;
import com.intellij.psi.PsiExpression;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
 * @author Ye Junhui
 * @since 2024/3/6
 */
public class PsiCollectionUtils {

    /**
     * 从代码块中寻找map成员
     *
     * @param collectionVariable
     * @param psiCodeBlock
     * @return
     */
    public static List<IColMember> findCollectionMemberInCodeBlock(IVariable collectionVariable, @Nullable PsiCodeBlock psiCodeBlock) {
        return PsiColUtils.findColMemberInCodeBlock(
                collectionVariable,
                psiCodeBlock,
                ColOperationFactory.COLLECTION_READ_WRITE_OPERATIONS,
                CollectionMemberResolverFactory.getInstance()
        );
    }

    /**
     * 寻找集合定义
     *
     * @param psiExpression
     * @return
     */
    @Nullable
    public static IColDeclaration findCollectionExpressionDeclaration(PsiExpression psiExpression) {
        return PsiColUtils.findColExpressionDeclaration(psiExpression);
    }
}
